package screens
{
	import flash.display.Bitmap;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import objects.Star_Effect;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.extensions.PDParticleSystem;
	import starling.filters.BlurFilter;
	import starling.filters.ColorMatrixFilter;
	import starling.text.TextField;
	import starling.textures.Texture;
	import Assets;
	import objects.ManagerObject;
	import objects.ManagerEffect;
	import events.NavigationEvent;
	import starling.events.EnterFrameEvent;
	
	/**
	 * ...
	 * @author Tuan
	 */
	public class ChooseStage extends Sprite
	{
		public var m_background:Image;
		public var m_pointTouch:Point;
		
		public var m_backBase:Image;
		public var m_ImageBase:Image;
		public var m_ImageButtonLeft:Image;
		public var m_ImageButtonBack:Image;
		public var m_ImageButtonRight:Image;
		public var m_TestMoveLeft:Boolean;
		public var m_TestMoveRight:Boolean;
		public var m_Count_Stage:int;
		public var m_ImageName:Image;
		public var m_ImageStart:Image;
		public var m_TextName:TextField;
		public var m_TextTitle:TextField;
		public var m_TextStart:TextField;
		
		public var m_StageImageArray:Array;
		
		public var m_maxStage:int;
		public var m_curStage:int;
		public var colorMatrixFilter:ColorMatrixFilter;
		
		public var m_testStart:Boolean;
		public var m_testInit:Boolean;
		
		public var m_testMoveStart:Boolean;
		
		public var m_rung:int;
		public var m_rung1:int;
		public var m_rung2:int;
		public var m_rung3:int;
		public function ChooseStage()
		{
			this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(starling.events.Event.ENTER_FRAME, update);
			this.addEventListener(TouchEvent.TOUCH, onTouch);
			super();
			
		
		}
		
		private function onTouch(e:TouchEvent):void
		{
			var touch:Touch = e.getTouch(stage);
			if (touch && m_testStart == true)
			{
				var m_touchPoint:Point = new Point(touch.globalX, touch.globalY);
				if (touch.phase == TouchPhase.BEGAN)
				{
					if (m_ImageButtonLeft.bounds.containsPoint(m_touchPoint) == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						if (m_Count_Stage < 3 && m_TestMoveLeft == false)
						{
							m_TestMoveLeft = true;
							m_Count_Stage += 1;
						}
					}
					if (m_ImageButtonRight.bounds.containsPoint(m_touchPoint) == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						if (m_Count_Stage > 0 && m_TestMoveRight == false)
						{
							m_TestMoveRight = true;
							m_Count_Stage -= 1;
						}
					}
					if (m_ImageStart.bounds.containsPoint(m_touchPoint) == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						if (m_Count_Stage < m_maxStage)
						{
							database.instance.m_curStage = m_Count_Stage;
							this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "ChooseStage-ChooseLevel"}, true));
							
						}
					}
					
					if (m_ImageButtonBack.bounds.containsPoint(m_touchPoint) == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "ChooseStage-Menu"}, true));
					}
				}
				else if (touch.phase == TouchPhase.HOVER)
				{
					if (m_ImageStart.bounds.containsPoint(m_touchPoint) == true)
					{
						m_ImageStart.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_ImageStart.filter = null;
					
					if (m_ImageButtonLeft.bounds.containsPoint(m_touchPoint) == true)
					{
						m_ImageButtonLeft.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_ImageButtonLeft.filter = null;
					
					if (m_ImageButtonRight.bounds.containsPoint(m_touchPoint) == true)
					{
						m_ImageButtonRight.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_ImageButtonRight.filter = null;
					if (m_ImageButtonBack.bounds.containsPoint(m_touchPoint) == true)
					{
						m_ImageButtonBack.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_ImageButtonBack.filter = null;
				}
			}
		}
		
		private function init():void
		{
			ManagerEffect.instance.m_layer = this;
			m_rung = 5;
			m_rung1 = 5;
			m_rung2 = 5;
			m_rung3 = 5;
			m_testMoveStart = false;
			m_maxStage = database.instance.RESULTSTAGE[1];
			m_backBase = new Image(Assets.getTexture("KHUNGHINH"));
			m_background = new Image(Assets.getTexture("background3"));
			m_ImageBase = new Image(Assets.getTexture("KHUNGHINH"));
			m_ImageName = new Image(Assets.getTexture("Khung"));
			m_ImageButtonLeft = new Image(Assets.getTexture("Lui"));
			m_ImageButtonBack = new Image(Assets.getTexture("CHUAHOANTHANH"));
			m_ImageButtonRight = new Image(Assets.getTexture("Lui"));
			m_ImageStart = new Image(Assets.getTexture("Chon"));
			
			m_TextStart = new TextField(500, 50, "Chọn", "MyFontName", 30, 0x332616);
			m_TextTitle = new TextField(500, 50, "Chọn thời kỳ", "MyFontName", 30, 0xffffff);
			m_TextName = new TextField(500, 50, "Loạn 12 sứ quân", "MyFontName", 25, 0x332616);
			m_StageImageArray = new Array();
			m_TestMoveLeft = false;
			m_TestMoveRight = false;
			m_Count_Stage = 0;
			
			//base Image
			m_background.pivotX = m_background.width / 2;
			m_background.pivotY = 0;
			m_background.x = 1050 / 2;
			m_background.y = 0;
			
			//base Image
			m_backBase.pivotX = m_backBase.width / 2;
			m_backBase.pivotY = m_backBase.height / 2;
			m_backBase.x = 526;
			m_backBase.y = 254;
			
			m_ImageBase.pivotX = m_ImageBase.width / 2;
			m_ImageBase.pivotY = m_ImageBase.height / 2;
			//m_ImageBase.width = 600;
			//m_ImageBase.height = 400;
			m_ImageBase.x = 526;
			m_ImageBase.y = 254;
			
			m_ImageName.pivotX = m_ImageName.width / 2;
			m_ImageName.pivotY = m_ImageName.height / 2;
			m_ImageName.x = 1050 / 2;
			m_ImageName.y = 100;
			
			m_ImageButtonLeft.pivotX = m_ImageButtonLeft.width / 2;
			m_ImageButtonLeft.pivotY = m_ImageButtonLeft.height / 2;
			m_ImageButtonLeft.x = 930;
			m_ImageButtonLeft.y = 308;
			m_ImageButtonLeft.rotation = 3.14;
			
			m_ImageButtonBack.pivotX = m_ImageButtonBack.width / 2;
			m_ImageButtonBack.pivotY = m_ImageButtonBack.height / 2;
			m_ImageButtonBack.x = 1025;
			m_ImageButtonBack.y = 25;
			
			m_ImageButtonRight.pivotX = m_ImageButtonRight.width / 2;
			m_ImageButtonRight.pivotY = m_ImageButtonRight.height / 2;
			m_ImageButtonRight.x = 120;
			m_ImageButtonRight.y = 308;
			
			m_ImageStart.pivotX = m_ImageStart.width / 2;
			m_ImageStart.pivotY = m_ImageStart.height / 2;
			m_ImageStart.x = 525;
			m_ImageStart.y = 640;
			
			//text
			m_TextName.pivotX = m_TextName.width / 2;
			m_TextName.pivotY = m_TextName.height / 2;
			m_TextName.x = m_ImageName.x;
			m_TextName.y = 447;
			
			m_TextTitle.pivotX = m_TextTitle.width / 2;
			m_TextTitle.pivotY = m_TextTitle.height / 2;
			m_TextTitle.x = m_TextName.x;
			m_TextTitle.y = m_TextName.y - 50;
			
			m_TextStart.pivotX = m_TextStart.width / 2;
			m_TextStart.pivotY = m_TextStart.height / 2;
			m_TextStart.x = m_ImageStart.x;
			m_TextStart.y = m_ImageStart.y;
			this.addChild(m_backBase);
			//stageImage
			var _stageImage:Image = new Image(Assets.getTexture("LOAN12SUQUAN"));
			_stageImage.pivotX = _stageImage.width / 2;
			_stageImage.pivotY = _stageImage.height / 2;
			_stageImage.x = m_ImageBase.x;
			_stageImage.y = m_ImageBase.y;
			this.addChild(_stageImage);
			m_StageImageArray.push(_stageImage);
			
			_stageImage = new Image(Assets.getTexture("SONGNHUNGUYET"));
			_stageImage.pivotX = _stageImage.width / 2;
			_stageImage.pivotY = _stageImage.height / 2;
			_stageImage.x = m_ImageBase.x + _stageImage.width;
			_stageImage.y = m_ImageBase.y;
			_stageImage.scaleX = 0.4;
			_stageImage.scaleY = 0.4;
			this.addChild(_stageImage);
			m_StageImageArray.push(_stageImage);
			
			_stageImage = new Image(Assets.getTexture("SOMRAMAT"));
			_stageImage.pivotX = _stageImage.width / 2;
			_stageImage.pivotY = _stageImage.height / 2;
			_stageImage.x = m_ImageBase.x + _stageImage.width * 2;
			_stageImage.y = m_ImageBase.y;
			_stageImage.scaleX = -0.2;
			_stageImage.scaleY = -0.2;
			this.addChild(_stageImage);
			m_StageImageArray.push(_stageImage);
			
			_stageImage = new Image(Assets.getTexture("SOMRAMAT"));
			_stageImage.pivotX = _stageImage.width / 2;
			_stageImage.pivotY = _stageImage.height / 2;
			_stageImage.x = m_ImageBase.x + _stageImage.width * 3;
			_stageImage.y = m_ImageBase.y;
			_stageImage.scaleX = -0.8;
			_stageImage.scaleY = -0.8;
			this.addChild(_stageImage);
			m_StageImageArray.push(_stageImage);
			
			this.addChild(m_background);
			//this.addChild(m_ImageName);
			this.addChild(m_ImageButtonLeft);
			this.addChild(m_ImageButtonRight);
			this.addChild(m_ImageStart);
			this.addChild(m_TextName);
			//this.addChild(m_TextTitle);
			this.addChild(m_TextStart);
			//this.addChild(m_ImageBase);
			this.addChild(m_ImageButtonBack);
			SetStage();
			
			setVisibleStage();
			if (database.instance.m_testPlayMusic == true)
				database.instance.m_Music.playSound("ChooseStage_Music", 1.0, 999);
			
		}
		
		private function onAddedToStage(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		public function SetStage():void
		{
			for (var i:int = 0; i < m_StageImageArray.length; i++)
			{
				if (i >= m_maxStage)
				{
					colorMatrixFilter = new ColorMatrixFilter();
					colorMatrixFilter.adjustSaturation(-1); // make image Grayscale
					m_StageImageArray[i].filter = colorMatrixFilter;
				}
			}
		}
		
		private function SetName(_countStage:int):void
		{
			switch (_countStage)
			{
				case 0: 
					m_TextName.text = "Loạn 12 sứ quân";
					break;
				case 1: 
					m_TextName.text = "Chiến thắng sông Như Nguyệt";
					break;
				case 2: 
					m_TextName.text = "Chống quân Mông Nguyên lần 1";
					break;
				case 3: 
					m_TextName.text = "Chống quân Mông Nguyên lần 2";
					break;
			}
		}
		
		private function MoveStageImageLeft(_countStage:int):void
		{
			if (m_StageImageArray[_countStage].x > m_ImageBase.x)
			{
				for (var i:int = 0; i < m_StageImageArray.length; i++)
				{
					m_StageImageArray[i].visible = true;
					m_StageImageArray[i].x -= 10;
					if (i < _countStage)
					{
						m_StageImageArray[i].scaleX -= 0.01;
						m_StageImageArray[i].scaleY -= 0.01;
					}
					else
					{
						m_StageImageArray[i].scaleX += 0.01;
						m_StageImageArray[i].scaleY += 0.01;
					}
					
				}
			}
			else
			{
				setVisibleStage();
				m_TestMoveLeft = false;
				SetName(_countStage);
			}
		
		}
		
		private function MoveStageImageRight(_countStage:int):void
		{
			if (m_StageImageArray[_countStage].x < m_ImageBase.x)
			{
				for (var i:int = 0; i < m_StageImageArray.length; i++)
				{
					m_StageImageArray[i].visible = true;
					m_StageImageArray[i].x += 10;
					if (i <= _countStage)
					{
						m_StageImageArray[i].scaleX += 0.01;
						m_StageImageArray[i].scaleY += 0.01;
					}
					else
					{
						m_StageImageArray[i].scaleX -= 0.01;
						m_StageImageArray[i].scaleY -= 0.01;
					}
				}
			}
			else
			{
				setVisibleStage();
				m_TestMoveRight = false;
				SetName(_countStage);
			}
		}
		
		private function update(e:Event):void
		{
			ManagerEffect.instance.update(Global.FRAME);
			if (m_testInit == true)
			{
				init();
				m_testInit = false;
				setDefaultSprite();
			}
			
			if (m_testStart == true)
			{
				if (m_testMoveStart == false)
				{
					for (var i:int = 0; i < database.instance.RESULTSTAGE[1] - 1; i++)
					{
						if (m_Count_Stage < database.instance.RESULTSTAGE[1] - 1 && m_TestMoveLeft == false)
						{
							m_TestMoveLeft = true;
							m_Count_Stage += 1;
						}
					}
					if (m_Count_Stage == database.instance.RESULTSTAGE[1] - 1)
						m_testMoveStart = true;
				}
				ZoomSprite(this);
				if (this.scaleX == 1)
				{
					m_rung = rung(this, m_rung,0);
					if (m_rung == 0)
					{
						ZoomSprite(m_ImageStart);
						ZoomSprite(m_TextStart);
						if (m_ImageStart.scaleX == 1)
						{
							m_rung1 = rung(m_ImageStart, m_rung1,1);
							m_TextStart.x = m_ImageStart.x;
							m_TextStart.y = m_ImageStart.y;
						}
						if (m_rung1 == 0)
						{
							ZoomSprite(m_ImageButtonLeft);
							ZoomSprite(m_ImageButtonRight);
							if (m_ImageButtonRight.scaleX == 1)
							{
								m_rung2 = rung(m_ImageButtonLeft, m_rung2,1);
								m_rung3 = rung(m_ImageButtonRight, m_rung3,1);
							}
						}
					}
				}
				if (m_TestMoveLeft == true)
					MoveStageImageLeft(m_Count_Stage);
				if (m_TestMoveRight == true)
					MoveStageImageRight(m_Count_Stage);
				if (m_Count_Stage >= m_maxStage)
				{
					colorMatrixFilter = new ColorMatrixFilter();
					colorMatrixFilter.adjustSaturation(-1); // make image Grayscale
					m_ImageStart.filter = colorMatrixFilter;
				}
			}
		}
		
		private function setVisibleStage():void
		{
			for (var i:int = 0; i < m_StageImageArray.length; i++)
			{
				if (i != m_Count_Stage)
					m_StageImageArray[i].visible = false;
				else
					m_StageImageArray[i].visible = true;
			}
		}
		
		private function ZoomSprite(obj:Object):void
		{
			if (obj.scaleX < 1)
			{
				obj.scaleX += 0.05;
				obj.scaleY = obj.scaleX;
			}
			else
			{
				obj.scaleX = 1;
				obj.scaleY = 1;
				
			}
		}
		public function rung(obj:Object, _rung:int,_buzz:int):int
		{
			if (_rung != 0)
				{
					if (_rung > 0)
					{
						_rung -= 1;
					}
					obj.x += _rung;
					obj.y -= _rung;
					_rung *= -1;
					if(_rung==4&&_buzz==1)
						ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML()), Texture.fromBitmap(new AssetsParticles.ParticleTexture())), new Point(obj.x, obj.y), 50));
					if(_rung==4&&_buzz==2)
						ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML1()), Texture.fromBitmap(new AssetsParticles.ParticleTexture1())), new Point(obj.x, obj.y), 50));
					return _rung;
				}
				return _rung;
		}
		
		private function setDefaultSprite():void
		{
			this.width = 1050;
			this.height = 700;
			this.pivotX = this.width / 2;
			this.pivotY = this.height / 2;
			this.x = this.width / 2;
			this.y = this.height / 2;
			this.scaleX = 0.1;
			this.scaleY = 0.1;
			m_ImageStart.scaleX = 0;
			m_ImageStart.scaleY = 0;
			m_TextStart.scaleX = 0;
			m_TextStart.scaleY = 0;
			
			m_ImageButtonLeft.scaleX = 0;
			m_ImageButtonLeft.scaleY = 0;
			
			m_ImageButtonRight.scaleX = 0;
			m_ImageButtonRight.scaleY = 0;
			
		}
		
		public function disposeTemporarily():void
		{
			ManagerEffect.instance.ListEff.splice(0, ManagerEffect.instance.ListEff.length);
			database.instance.m_Music.stopSound("ChooseStage_Music");
			this.removeChildren(0, this.numChildren, false);
			this.scaleX = 0.1;
			this.scaleY = 0.1;
			this.m_testStart = false;
			this.visible = false;
		}
	}
}